package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.ada.base.common.ResultData;
import com.smartfactory.sail.config.HeaderWrapper;
import com.smartfactory.sail.constant.header.ProductEnum;
import com.smartfactory.sail.dto.ContractAmountDto;
import com.smartfactory.sail.entity.Contract;
import com.smartfactory.sail.service.IContractService;
import com.smartfactory.sail.vo.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 合同表 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Api(tags ="合同相关")
@RestController
@RequestMapping("/contract")
public class ContractController extends ControllerResponse {

    private IContractService iContractService;

    public ContractController(IContractService iContractService) {
        this.iContractService = iContractService;
    }

    @PostMapping("add")
    @ApiOperation(value = "合同新增",notes = "合同新增")
    public ApiResponse<? extends Object> add(@RequestBody Contract contract) {
        if (iContractService.save(contract))
            return ok(contract.getId());
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @ApiOperation(value = "根据id获取核合同详情", notes = "根据id获取核合同详情")
    @GetMapping("info/{id}")
    public ApiResponse<Contract> getById(@PathVariable Long id) {
        return ok(iContractService.getById(id));
    }

    @HeaderWrapper(ProductEnum.class)
    @ApiOperation(value = "合同列表 分页|currentPage|size|sortType|sortField 必传", notes = "产品列表 分页|currentPage|size|sortType|sortField 必传")
    @PostMapping("listProduct/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<Object> list(@RequestBody Map<String ,Object> queryParam,
                                           @PathVariable("currentPage")Long currentPage,
                                           @PathVariable("size")Integer size,
                                           @PathVariable("sortType")Integer sortType,
                                           @PathVariable("sortField")String sortField) {
        PageParam pageParam = new PageParam(currentPage,size,sortType,sortField);
        queryParam.put("itemMode", 1);
        return  ok(iContractService.listContract(queryParam,pageParam));
    }



}
